package com.linwen.excise.od;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;

/**
 * @ClassName _社交距离
 * @Description TODO
 * @Author zero
 * @DATE 2024/4/10 10:41 PM
 * @Version 1.0
 */
public class _社交距离 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int seatNum = sc.nextInt();
        sc.nextLine();
        String seatOrLeaveLine = sc.nextLine();
        String[] c = seatOrLeaveLine.substring(1,seatOrLeaveLine.length()-1).split(",");
        int[] seatOrLeave = new int[c.length];
        for (int i = 0; i < c.length; i++) {
            seatOrLeave[i] = Integer.parseInt(c[i]);
        }

        _社交距离 socialDistance = new _社交距离();
        int ans = socialDistance.conferenceSeatDistance(seatNum, seatOrLeave);
        System.out.println(ans);
    }

    /**
     * 计算最后进来的人，坐的位置
     * @param seatNum 会议室座位总数
     * @param seatOrLeave 员工的进出顺序
     * @return 最后进来的人，坐的位置
     */
    public int conferenceSeatDistance(int seatNum, int[] seatOrLeave) {
        HashSet<Integer> seatTable = new HashSet<>();
        for (int i = 0; i < seatOrLeave.length; i++) {
            if(seatOrLeave[i]>0){
                if(seatTable.size()==0){
                    seatTable.add(0);
                    if(i==seatOrLeave.length-1){
                        return 0;
                    }
                }else if(seatTable.size()==1){
                    seatTable.add(seatNum-1);
                    if(i==seatOrLeave.length-1){
                        return seatNum-1;
                    };
                }else if(seatTable.size()<seatNum){
                    int[] arr = new int[seatTable.size()];
                    int index = 0;
                    for (Integer integer : seatTable) {
                        arr[index++] = integer;
                    }
                    Arrays.sort(arr);
                    int start = 0;
                    int maxLength = 0;
                    for (int j = 0; j < arr.length-1; j++) {
                        int dis = (arr[j+1]-arr[j])/2;
                        if(dis>maxLength){
                            maxLength = dis;
                            start = arr[j];
                        }
                    }
                    seatTable.add(start+maxLength);
                    if(i==seatOrLeave.length-1){
                        return start+maxLength;
                    }
                }else {
                    return -1;
                }
            }else {
                seatTable.remove(-seatOrLeave[i]);
            }
        }
        return 0;
    }
}
